﻿/////////////////////////////////
////页面初始化API
function func_page_init()
{
    var url = 'api/add';

    $.ajax({
        dataType: "json",
        url: url,
        data:
            {
                id: pub_id,

                __d: (new Date()).valueOf()
            },
        //////////////////////////////////////////////////
        success: function (data, textStatus, jqXHR)
        {
            setTimeout(function ()
            {
                func_page_init__success(data);
            }, 100);
        },
        //////////////////////////////////////////////////
        error: function (data, textStatus, jqXHR)
        {
        }
    });
}


function func_page_init__success(data)
{
    console.log(data);

    vm.cash_date = new Date();
    vm.type = "";
    vm.ab_type = "-";
    vm.num = ""
    vm.desc = "";


    var entity_cashbook = data.entity_cashbook;
    if (entity_cashbook)
    {
        //数据初始化

        vm.cash_date = entity_cashbook.cashrec_date.split(' ')[0];
        vm.type = entity_cashbook.cashrec_type;
        vm.ab_type = entity_cashbook.cashrec_ab_type;

        vm.num = entity_cashbook.cashrec_num;

        vm.desc = entity_cashbook.cashrec_desc;


        //$(".ab_type").val(vm.ab_type);
        //$("#compNum").val(vm.num);
        //$("#compDesc").val(vm.desc);
    }


    for (var key in data.types)
    {
        var html = `<div class="col-xs-3 text-center {$class}" data-type="{$key}">{$type}</div>`;

        var type_ = data.types[key];
        if (vm.type == key)
        {
            html = html.replace("{$class}", "select");
        }
        else
        {
            html = html.replace("{$class}", "");
        }

        html = html.replace("{$type}", type_)
            .replace("{$key}", key)
        ;

        $(".type").append(html);
    }


    $(".type div").click(function ()
    {
        var class_name = 'select';


        if (!$(this).hasClass(class_name))
        {
            //alert($(this).attr("data-type"));

            $(".type div").removeClass(class_name);
            $(this).addClass(class_name);
        }
        else
        {
            $(this).removeClass(class_name);
        }
    });
}


$(function ()
{
    $(".btn-success").click(function ()
    {
        var date = moment(vm.cash_date).format("YYYY-MM-DD");
        var desc = vm.desc;
        var ab_type = vm.ab_type;

        var type = $(".type .select").attr("data-type");
        var num = vm.num;


        if (typeof (type) == "undefined")
        {
            func_alert("请选择类型");
            return;
        }

        if (num.length <= 0)
        {
            func_alert("请填写金额");
            return;
        }


        func_cashbook_add(date, type, ab_type, num, desc);
    });
});

function func_cashbook_add__success(data, textStatus, jqXHR)
{
    console.log(data);
    console.log(textStatus);
    console.log(jqXHR);

    if (data.errcode == 0)
    {
        location.href = "index.html";
    }
    else
    {
        func_alert(data.errmsg);
    }
}


$(function ()
{
    $(".date").mobiscroll().date({
        theme: "android-ics light",
        lang: "zh",
        cancelText: null,
        dateFormat: 'yy-mm-dd',
        animate: "flip",
        headerText: function (valueText)
        {
            //自定义弹出框头部格式
            array = valueText.split('-');
            var date_str = array[0] + "年" + array[1] + "月" + array[2] + "日";
            $(".date").html(valueText);

            vm.cash_date = new Date(valueText);

            return date_str;
        }
    });
})